package com.zd.shopping.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zd.shopping.R;
import com.zd.shopping.apis.NuclearGoodsDetailsApi;
import com.zd.shopping.apis.NuclearGoodsSeveApi;
import com.zd.shopping.baseadapter.NuclearGoodsAdapter;
import com.zd.shopping.baseadapter.NuclearGoodsDetailsAdapter;
import com.zwz.mall.api.OnRequestListener;
import com.zwz.mall.base.BaseActivity;
import com.zwz.mall.utils.BarHeightUtils;
import com.zwz.mall.utils.LoadingDialog;
import com.zwz.mall.utils.ToastUtils;

import butterknife.BindView;

/**
 *
 * @author Administrator
 * @date 2019/10/30 0030
 */

public class NumclearDetailsActivity extends BaseActivity
{
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.tv_seve)
    TextView mTvseve;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.enpty)
    RelativeLayout enpty;
 private  NuclearGoodsDetailsApi nuclearGoodsDetailsApi =  new NuclearGoodsDetailsApi();
 private NuclearGoodsSeveApi nuclearGoodsSeveApi =  new NuclearGoodsSeveApi();
    private NuclearGoodsDetailsAdapter nuclearGoodsDetailsAdapter;
    private String id;

    @Override
    public int getLayoutId()
    {
        return R.layout.activity_details;
    }

    @Override
    public void initPresenter()
    {

    }

    @Override
    public void initView()
    {
        BarHeightUtils.setStatusBarHeight(fakeStatusBar, getStatusBarHeight());
        id = getIntent ().getStringExtra ("id");
        nuclearGoodsDetailsApi.omnoticeid= id;
        mRecyclerView.setLayoutManager(new LinearLayoutManager (this));
        nuclearGoodsDetailsAdapter = new NuclearGoodsDetailsAdapter (this, nuclearGoodsDetailsApi.list);
        mRecyclerView.setAdapter(nuclearGoodsDetailsAdapter);
        title.setText ("商品详情");
        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener ()
        {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout)
            {
                //下拉刷新,一般添加调用接口获取数据的方法
//                oprateListApi.page = 0;
                intData();
            }

            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout)
            {
                //上拉加载，一般添加调用接口获取更多数据的方法
                refreshLayout.setNoMoreData(true);

            }
        });

    }
    private void intData() {
        if (nuclearGoodsDetailsApi.isLoading())
        {
            return;
        }
        nuclearGoodsDetailsApi.list.clear();
        nuclearGoodsDetailsAdapter.notifyDataSetChanged();
        nuclearGoodsDetailsApi.loadData (new OnRequestListener () {
            @Override
            public void onBefore() {

            }

            @Override
            public void onSuccess() {
                nuclearGoodsDetailsAdapter.notifyDataSetChanged();
                mRefreshLayout.finishRefresh();
                mRefreshLayout.finishLoadMore();
            }

            @Override
            public void onFailure(Throwable error) {
                mRefreshLayout.finishRefresh();
                mRefreshLayout.finishLoadMore();
            }

            @Override
            public void onNetWorkError() {

            }
        });
    }
    @Override
    public void fillData()
    {
        intData();
    }

    @Override
    public void setListener()
    {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTvseve.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                nuclearGoodsSeveApi.id=id;
                LoadingDialog.showDialogForLoading(mActivity, "保存中...");
                if (nuclearGoodsSeveApi.isLoading())
                {
                    return;
                }
                nuclearGoodsSeveApi.loadData (new OnRequestListener () {
                    @Override
                    public void onBefore() {

                    }

                    @Override
                    public void onSuccess() {
                        ToastUtils.showShort ("保存成功");
                        LoadingDialog.cancelDialogForLoading();
                        finish ();
                    }

                    @Override
                    public void onFailure(Throwable error) {
                        LoadingDialog.cancelDialogForLoading();
                    }

                    @Override
                    public void onNetWorkError() {

                    }
                });
            }
        });
    }
}
